package com.yc.shopping.ui;

import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;

import com.yc.shopping.dao.GoodTypeDao;
import com.yc.shopping.dao.GoodsDao;
import com.yc.shopping.util.ShoppingUtil;
import org.eclipse.swt.widgets.Label;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;

public class Dialog_AddGoods extends Dialog {

	protected Object result;
	protected Shell shell;
	private Text text_gname;
	private Text text_gcode;
	private Text text_unit;
	private Text text_weight;
	private Text text_gdate;
	private Text text_price;
	private Text text_salprice;
	private Text text_gmark;
	private Combo combo;
	private String fileName;

	GoodTypeDao typeDao =new GoodTypeDao();
	GoodsDao dao = new GoodsDao();
	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public Dialog_AddGoods(Shell parent, int style) {
		super(parent, style);
		setText("商品添加:");
	}

	/**
	 * Open the dialog.
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), getStyle());
		shell.setSize(562, 591);
		shell.setText("添加商品信息");
		//对话框居中
		ShoppingUtil.centerWindows(shell);
		
		Label label = new Label(shell, SWT.NONE);
		label.setBounds(36, 40, 61, 17);
		label.setText("商品类型：");
		//下拉列表作为全局变量
		combo = new Combo(shell, SWT.NONE);
		combo.setBounds(103, 37, 135, 25);
		combo.select(0);
		
		
		Label lblNewLabel = new Label(shell, SWT.NONE);
		lblNewLabel.setBounds(291, 40, 61, 17);
		lblNewLabel.setText("商品名称：");
		
		text_gname = new Text(shell, SWT.BORDER);
		text_gname.setBounds(358, 37, 135, 23);
		
		Label lblNewLabel_1 = new Label(shell, SWT.NONE);
		lblNewLabel_1.setBounds(36, 110, 61, 17);
		lblNewLabel_1.setText("条形码：");
		
		text_gcode = new Text(shell, SWT.BORDER);
		text_gcode.setBounds(103, 107, 135, 23);
		
		Label lblNewLabel_2 = new Label(shell, SWT.NONE);
		lblNewLabel_2.setBounds(291, 110, 61, 17);
		lblNewLabel_2.setText("商品单位：");
		
		text_unit = new Text(shell, SWT.BORDER);
		text_unit.setText("");
		text_unit.setBounds(358, 107, 135, 23);
		
		Label lblNewLabel_3 = new Label(shell, SWT.NONE);
		lblNewLabel_3.setBounds(36, 184, 61, 17);
		lblNewLabel_3.setText("商品净重：");
		
		text_weight = new Text(shell, SWT.BORDER);
		text_weight.setBounds(103, 178, 135, 23);
		
		Label label_1 = new Label(shell, SWT.NONE);
		label_1.setBounds(291, 184, 61, 17);
		label_1.setText("保质期：");
		
		text_gdate = new Text(shell, SWT.BORDER);
		text_gdate.setBounds(358, 178, 135, 23);
		
		Label lblNewLabel_4 = new Label(shell, SWT.NONE);
		lblNewLabel_4.setBounds(36, 253, 61, 17);
		lblNewLabel_4.setText("进价：");
		
		text_price = new Text(shell, SWT.BORDER);
		text_price.setText("");
		text_price.setBounds(103, 250, 135, 23);
		
		Label lblNewLabel_5 = new Label(shell, SWT.NONE);
		lblNewLabel_5.setBounds(291, 253, 61, 17);
		lblNewLabel_5.setText("售价：");
		
		text_salprice = new Text(shell, SWT.BORDER);
		text_salprice.setBounds(358, 250, 135, 23);
		
		Label label_2 = new Label(shell, SWT.NONE);
		label_2.setBounds(36, 307, 61, 17);
		label_2.setText("商品描述：");
		
		text_gmark = new Text(shell, SWT.BORDER | SWT.MULTI);
		text_gmark.setBounds(103, 307, 135, 140);
		
		Label label_3 = new Label(shell, SWT.NONE);
		label_3.setBounds(291, 307, 61, 17);
		label_3.setText("商品图片：");
		
		Label label_image = new Label(shell, SWT.NONE);
		label_image.setImage(SWTResourceManager.getImage("D:\\eclipse\\Project\\YC123_Project\\src\\com\\yc\\shopping\\image\\zanwu.jpg"));
		label_image.setBounds(358, 307, 135, 140);
		
		Label label_4 = new Label(shell, SWT.NONE);
		//图片上传
		label_4.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDoubleClick(MouseEvent e) {
				
				FileDialog fd = new FileDialog(shell);
				fd.setFilterExtensions(new String[] {"*.jpg","*.png","*.jpeg","*.*"});
				String select = fd.open();
				System.out.println(select);
				
				if(null==select ||"".equals(select)) {
					ShoppingUtil.showMessage(shell, "消息提示", "未选中图片");
					return;
				}
				
				try {
					File file=new File(select);
					fileName= file.getName();
					InputStream in = new FileInputStream(file);	
					Image image =new Image(Display.getDefault(),in);
					label_image.setImage(image);
				} catch (FileNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
				
				
				
			}
		});
		label_4.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 10, SWT.NORMAL));
		label_4.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
		label_4.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_LIGHT_SHADOW));
		label_4.setBounds(389, 453, 78, 17);
		label_4.setText("双击上传图片");
		
		Button button = new Button(shell, SWT.NONE);
		//添加按钮
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				//从界面上获取数据
				String type = combo.getText().trim();
				String tid = type.split("-")[0];
				if(null == fileName ||"".equals(fileName)) {
					ShoppingUtil.showMessage(shell, "消息提示","未上传图片");
					return;
				}
				String gname =text_gname.getText().trim();
				String gcode = text_gcode.getText().trim();
				String gdate = text_gdate.getText().trim();
				String price = text_price.getText().trim();
				String salprice = text_salprice.getText().trim();
				String gmark =text_gmark.getText().trim();
				String unit = text_unit.getText().trim();
				String weight =text_weight.getText().trim();
				String gimage ="D:/eclipse/Project/YC123_Project/src/com/yc/shopping/image"+fileName;
				try {
					
					int result = dao.add(gname, gcode, tid, gmark, gimage, unit, 
							weight, 0, gdate, Float.parseFloat(price), Float.parseFloat(salprice));
					if(result<=0) {
						ShoppingUtil.showMessage(shell, "消息提示", "商品录入失败");
						return;
					}else {
						ShoppingUtil.showMessage(shell, "消息提示", "商品录入成功");
					}
					//清空所有文本图片
					text_gcode.setText("");
					text_gdate.setText("");
					text_gmark.setText("");
					text_gname.setText("");
					text_price.setText("");
					text_salprice.setText("");
					text_unit.setText("");
					text_weight.setText("");
					//图片清空
					label_image.setImage(SWTResourceManager.getImage("D:\\eclipse\\Project\\YC123_Project\\src\\com\\yc\\shopping\\image\\zanwu.jpg"));

				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
			}
		});
		button.setBounds(242, 488, 80, 27);
		button.setText("添加");
		
		showTypes();
	}
	
	/**
	 * 下拉列表
	 */
	
	private void showTypes() {
		try {
			List<Map<String,Object>> list = typeDao.findAll();
			
			String []  infos= new String[list.size()];
			for (int i = 0; i <list.size(); i++) {
				Map<String, Object> m = list.get(i);
				//下拉列表 1 - 肉类
				infos[i] = ShoppingUtil.objToString(m.get("TID")+"-"+ShoppingUtil.objToString(m.get("TNAME")));
				
			}		
			//设置到下拉列表中
			combo.removeAll();//清楚下拉列表中数据
			combo.setItems(infos);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}
